home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / usenet / sources / volume91 / utilitys / sman_1_0 / part01 / src / sless.c < prev    next >
C/C++ Source or Header  |  1991-05-18  |  6KB  |  339 lines

  1.  
  2. #include "sless_defs.h"
  3. #include <libraries/sregexpbase.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include <ctype.h>
  7. #include <clib/dos_protos.h>
  8.  
  9. Prototype void            usage(void);
  10. Prototype void            refreshscreen(void);
  11. Prototype void            waitcommand(void);
  12. Prototype void            leave(int, char *);
  13. Prototype int            putline(void);
  14.  
  15. const char memory[] = "sless: Unable to allocate memory.\n";
  16. const char end[]    = " \x9b7m-- END OF FILE --\x9b0m";
  17. const char top[]    = " \x9b7m-- TOP OF FILE --\x9b0m";
  18. const char nopat[]  = " \x9b7m-- Pattern not found --\x9b0m";
  19. const char nolast[] = " \x9b7m-- No previous pattern --\x9b0m";
  20. const char wait[]   = " \x9b7m-- Searching --\x9b0m";
  21. const char abort[]  = " \x9b7m-- Search aborted --\x9b0m";
  22.  
  23. char seek = 0;
  24. long startpos = 0,nextpos = 0;
  25. FILE *file = NULL,*ofile = NULL;
  26. BPTR con = 0;
  27. int sx,sy;
  28. struct SregExp *lastspat = NULL;
  29.  
  30. int main(ac,av)
  31. int ac;
  32. char **av;
  33. {
  34.     int line = 0,i;
  35.     char *p,*oldwin = NULL;
  36.  
  37.     show_line;
  38.     while (ac > 0) {
  39.     ac--;
  40.     av++;
  41.     if (**av != '-')
  42.         break;
  43.     switch (*(*av+1)) {
  44.         case ('l') :
  45.         if (line != 0)
  46.             usage();
  47.         p = *av+2;
  48.         while (isdigit(*p))
  49.             line = line*10 + *p++ - '0';
  50.         break;
  51.         case ('c') :
  52.         if (line != 0)
  53.             usage();
  54.         p = *av+2;
  55.         while (isdigit(*p))
  56.             line = line*10 - *p++ + '0';
  57.         break;
  58.         default :
  59.         usage();
  60.     }
  61.     }
  62.  
  63.     if (ac < 1)
  64.     usage();
  65.     if (!(file = sopen(*av)))
  66.     leave(15,"Unable to open input stream.\n");
  67.  
  68.     /* try and find the length of a file.  If it fails or is zero, then
  69.        we assume we cannot seek into the file.    This catches trying to
  70.        seek into pipes. */
  71.     sseek(file,0,SEEK_END);
  72.     if (stell(file) <= 0)
  73.     seek = 0;
  74.     else {
  75.     seek = 1;
  76.     sseek(file,0,SEEK_SET);
  77.     }
  78.  
  79.     con = makeoutput(*av);
  80.  
  81.     if (line < 0) {
  82.     startpos = -line;
  83.     if (seek)
  84.         sseek(file,startpos,SEEK_SET);
  85.     else
  86.         for (i = 0; i > line; i--)
  87.         getc(file);
  88.     } else {
  89.     for (i = 0; i < line && (p = sgetline(file)); i++) {
  90.         show_line;
  91.         free(p);
  92.         show_line;
  93.         if (seek)
  94.         startpos = stell(file);
  95.     }
  96.     }
  97.  
  98.     findsize(&sx,&sy,con);
  99.     refreshscreen();
  100.     waitcommand();
  101.  
  102.     closeoutput(con);
  103.     con = 0;
  104.     sclose(file);
  105.     file = NULL;
  106.     return 0;
  107. }
  108.  
  109. void
  110. usage()
  111. {
  112.     show_line;
  113.     sputs("sless V"\
  114.       VERSION\
  115.       ", "\
  116.       __DATE__\
  117.       ", \xA9Copyright 1991 by Jon Spencer.\n");
  118.     sputs("Usage: sless [-llineno | -ccharpos] file\n");
  119.     sputs("Where lineno is the optional starting line number,\n");
  120.     sputs("or charpos is the optional starting character positon.\n");
  121.     exit(2);
  122. }
  123.  
  124. void
  125. leave(x,p)
  126. int x;
  127. char *p;
  128. {
  129.     show_line;
  130.     sputs(p);
  131.     closeoutput(con);
  132.     if (con)
  133.     closeoutput(con);
  134.     if (file)
  135.     sclose(file);
  136.     if (ofile)
  137.     sclose(ofile);
  138.     exit(x);
  139. }
  140.  
  141. void
  142. waitcommand()
  143. {
  144.     short c;
  145.  
  146.     show_line;
  147.     do {
  148.     switch (c = getconchar(con)) {
  149.         case (' ') :
  150.         sgetcur(file);
  151.         if (seof(file) && nextpos > 0)
  152.             nextpos = -1;
  153.         if (nextpos >= 0) {
  154.             startpos = nextpos;
  155.             refreshscreen();
  156.         } else {
  157.             bottomline();
  158.             if (nextpos-- == -2)
  159.             return;
  160.             putconstr(end,con);
  161.         }
  162.         break;
  163.         case ('<') :
  164.         case (makeconid('T',0)) :
  165.         if (seek) {
  166.             if (startpos == 0) {
  167.             bottomline();
  168.             putconstr(top,con);
  169.             } else {
  170.             startpos = 0;
  171.             refreshscreen();
  172.             }
  173.         }
  174.         break;
  175.         case (13) :
  176.         case (makeconid('B',0)) :
  177.         sgetcur(file);
  178.         if (seof(file) && nextpos > 0)
  179.             nextpos = -1;
  180.         if (nextpos >= 0) {
  181.             downone();
  182.         } else {
  183.             bottomline();
  184.             if (nextpos-- == -2)
  185.             return;
  186.             putconstr(end,con);
  187.         }
  188.         break;
  189.         case ('q') :
  190.         bottomline();
  191.         return;
  192.         case (8) :
  193.         case ('b') :
  194.         if (seek) {
  195.             if (startpos == 0) {
  196.             bottomline();
  197.             putconstr(top,con);
  198.             } else
  199.             backup(sy-2);
  200.         }
  201.         break;
  202.         case ('d') :
  203.         sgetcur(file);
  204.         if (seof(file) && nextpos > 0)
  205.             nextpos = -1;
  206.         if (nextpos >= 0) {
  207.             int i = 1;
  208.  
  209.             while (i++ < sy/2 && nextpos > 0)
  210.             startpos = skiponeline(startpos);
  211.             refreshscreen();
  212.         } else {
  213.             bottomline();
  214.             if (nextpos-- == -2)
  215.             return;
  216.             putconstr(end,con);
  217.         }
  218.         break;
  219.         case ('>') :
  220.         case (makeconid('S',0)) :
  221.         if (seek) {
  222.             sseek(file,-1,SEEK_END);
  223.             startpos = stell(file);
  224.             backup(sy-1);
  225.         }
  226.         break;
  227.         case ('u') :
  228.         if (seek) {
  229.             if (startpos != 0) {
  230.             backup(sy/2-1);
  231.             } else {
  232.             bottomline();
  233.             putconstr(top,con);
  234.             }
  235.         }
  236.         break;
  237.         case (makeconid('A',0)) :
  238.         if (seek) {
  239.             if (startpos != 0) {
  240.             uponeline();
  241.             } else {
  242.             bottomline();
  243.             putconstr(top,con);
  244.             }
  245.         }
  246.         break;
  247.         case ('/') :
  248.         if (seek) {
  249.             bottomline();
  250.             putconstr("/",con);
  251.             if (!searchfor())
  252.             break;
  253.             nextpat();
  254.         }
  255.         break;
  256.         case ('n') :
  257.         if (seek)
  258.             nextpat();
  259.         break;
  260.         case ('p') :
  261.         if (seek)
  262.             prevpat();
  263.         break;
  264.         case (';') :
  265.         if (seek) {
  266.             bottomline();
  267.             putconstr(";",con);
  268.             if (!searchfor())
  269.             break;
  270.             prevpat();
  271.         }
  272.         break;
  273.         case (makeconid('?','~')) :
  274.         case ('h') :
  275.         case ('?') :
  276.         if (!ofile)
  277.             behelpfull();
  278.         break;
  279.     }
  280.     } while (TRUE);
  281. }
  282.  
  283. void
  284. refreshscreen()
  285. {
  286.     int y,c;
  287.     long t;
  288.  
  289.     show_line;
  290.     putconstr("\x9b0 p\x0c",con);
  291.  
  292.     y = sy;
  293.     if (seek)
  294.     sseek(file,startpos,SEEK_SET);
  295.     do {
  296.     if (seek && y > 1)
  297.         nextpos = stell(file);
  298.     } while (--y > 0 && !(c = putline()));
  299.  
  300.     if (seof(file))
  301.     nextpos = -1;
  302.     if (c == 2) {
  303.     nextpos = stell(file);
  304.     }
  305.  
  306.     while (--y > 0)
  307.     putconchar(10,con);
  308.  
  309.     putconstr(": \x9b p",con);
  310. }
  311.  
  312. int
  313. putline()
  314. {
  315.     char *p,*q;
  316.  
  317.     show_line;
  318.  
  319.     if (!(p = sgetline(file))) {
  320.     putconchar(10,con);
  321.     return TRUE;
  322.     }
  323.     q = skipdispline(p);
  324.     if (*(q-1) == 12) {
  325.     *(q-1) = 0;
  326.     putconstr(p,con);
  327.     putconstr("^L\n",con);
  328.     free(p);
  329.     return 2;
  330.     } else if (*q) {
  331.     sseek(file,q - p - strlen(p),SEEK_CUR);
  332.     *q++ = '\n';
  333.     *q = 0;
  334.     }
  335.     putconstr(p,con);
  336.     free(p);
  337.     return seof(file);
  338. }
  339.